package com.verisette.ncos.persistence.dao;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.corp.agi.common.bean.PagingBean;
import org.corp.agi.persistence.dao.CommonDAOImpl;

import com.verisette.ncos.persistence.domain.CustomLogAuditLog;

public class AuditLogDAOImpl extends CommonDAOImpl implements AuditLogDAO {

	@Override
	public List<CustomLogAuditLog> inquiryAuditLog(Date fromDate, Date toDate, Integer uspId, String status, PagingBean pagingBean) {
		List param = new LinkedList();
		List<CustomLogAuditLog> customLogAuditLogList = null;
		StringBuffer q1 = new StringBuffer();
		
		q1.append("SELECT LOG_AUDIT_LOG.ADL_ID, LOG_AUDIT_LOG.ADL_DATETIME, LOG_AUDIT_LOG.qsMatchSetID, MASTER_USER_PROFILE.USP_USER_NAME, LOG_AUDIT_LOG.STATUS, LOG_AUDIT_LOG.USP_ID");
		q1.append(" FROM LOG_AUDIT_LOG INNER JOIN MASTER_USER_PROFILE ON LOG_AUDIT_LOG.USP_ID = MASTER_USER_PROFILE.USP_ID");
		
		if(fromDate != null || toDate != null || uspId != null || (status != null && !"".equals(status))){
			// Have condition
			q1.append(" WHERE");
			
			if(uspId != null){
				q1.append(" MASTER_USER_PROFILE.USP_ID = ? AND");
				param.add(uspId);
			}
			
			if(status != null && !"".equals(status)){
				q1.append(" LOG_AUDIT_LOG.STATUS = ? AND");
				param.add(status);
			}
			
			if(fromDate != null && toDate != null){
				q1.append(" LOG_AUDIT_LOG.ADL_DATETIME BETWEEN ? AND ?");
				param.add(fromDate);
				param.add(toDate);
			}else{
				// remove "AND" string
				q1.replace(q1.length()-4, q1.length(), "");
				
			}
			
			q1.append(" order by ADL_DATETIME ");
			q1.append(pagingBean.getOrderMode());
			
		}
		
		customLogAuditLogList = (List<CustomLogAuditLog>)nativeQuery(q1.toString(), CustomLogAuditLog.class, pagingBean, param.toArray());
		
		return customLogAuditLogList;
	}

	@Override
	public List<CustomLogAuditLog> inquiryAuditLog(String qsMatchSetId, PagingBean pagingBean) {
		List param = new LinkedList();
		List<CustomLogAuditLog> customLogAuditLogList = null;
		StringBuffer q1 = new StringBuffer();
		
		q1.append("SELECT LOG_AUDIT_LOG.ADL_ID, LOG_AUDIT_LOG.ADL_DATETIME, LOG_AUDIT_LOG.qsMatchSetID, MASTER_USER_PROFILE.USP_USER_NAME, LOG_AUDIT_LOG.STATUS, LOG_AUDIT_LOG.USP_ID");
		q1.append(" FROM LOG_AUDIT_LOG INNER JOIN MASTER_USER_PROFILE ON LOG_AUDIT_LOG.USP_ID = MASTER_USER_PROFILE.USP_ID");
		q1.append(" WHERE LOG_AUDIT_LOG.qsMatchSetID = ?");
		q1.append(" order by ADL_DATETIME ");
		q1.append(pagingBean.getOrderMode());
		
		param.add(qsMatchSetId);
		
		customLogAuditLogList = (List<CustomLogAuditLog>)nativeQuery(q1.toString(), CustomLogAuditLog.class, pagingBean, param.toArray());
		
		return customLogAuditLogList;
	}
	
//	public void createAuditLog(Integer uspId, String groupId, String status, Date date) {
//		LogAuditLog auditLog = new LogAuditLog();
//		auditLog.setAdlDatetime(date);
//		auditLog.setGroupId(Integer.parseInt(groupId));
//		auditLog.setStatus(status);
//		auditLog.setUspId(uspId);
//		save(auditLog);
//	}
	
	public int createAuditLog(Long recordId, String status, String confirmFlag, String isOveride, Integer rjrId, Integer uspId, Integer seq) {
		StringBuffer q1 = new StringBuffer();
		List<Object> param = new LinkedList<Object>();
		q1.append("insert into LOG_AUDIT_LOG ");
		q1.append("(ADL_DATETIME,RECORD_ID,GROUP_ID,CLASS,[SYSTEM] ");
		q1.append(",CUSTOMER_NUMBER,ID_TYPE,ID_NUMBER,CDM_STATUS,CDM_STATUS_IN_GROUP,CASE_ID ");
		q1.append(",SUB_CASE_ID,ORIGINAL_QTY,EDITOR_USER_ID,APPROVER_USER_ID,STATUS_OR_ACTION ");
		q1.append(",EDIT_FLAG,CONFIRM_FLAG,IS_OVERIDE,RJR_ID,USP_ID,SEQ ");
		q1.append(") ");
		q1.append("select CURRENT_TIMESTAMP,RECORD_ID,GROUP_ID,CLASS,[SYSTEM] ");
		q1.append(",CUSTOMER_NUMBER, MAIN_ID_TYPE, MAIN_ID_NO,CDM_STATUS,CDM_STATUS_IN_GROUP,CASE_ID ");
		q1.append(",SUB_CASE_ID,TOTAL_RECORD_IN_GROUP,EDITOR_USER_ID,APPROVE_USER_ID,? ");
		param.add(status);
		q1.append(",EDIT_FLAG,?,?,?,?,? ");
		param.add(confirmFlag);param.add(isOveride);param.add(rjrId);param.add(uspId);param.add(seq);
		q1.append(" ");
		q1.append("from CUSTOMER_MASTER ");
		q1.append("where RECORD_ID = ? ");
		param.add(recordId);
		return executeNativeSQL(q1.toString(), param.toArray());
	}
	
	
	
}
